package com.ifd.core.api;

import org.apache.log4j.Logger;

import com.ibm.as400.access.AS400PackedDecimal;
import com.ibm.as400.access.AS400Text;
import com.ibm.as400.access.CommandCall;
import com.ibm.as400.access.ObjectDescription;
import com.ibm.as400.access.ProgramCall;
import com.ibm.as400.access.ProgramParameter;
import com.ibm.as400.access.QSYSObjectPathName;
import com.ifd.as400.IFDConnectionPool;
import com.ifd.as400.IFDException;

public class GenAS400Function {
	public static boolean qcmdexc(ProgramCall pgm, String cmd){
		ProgramParameter parms[] = new ProgramParameter[2];
		parms[0]=new ProgramParameter(new AS400Text(cmd.length(),pgm.getSystem()).toBytes(cmd));
		parms[1]=new ProgramParameter(new AS400PackedDecimal(15,5).toBytes(cmd.length()));
		String bckPgm = pgm.getProgram();
		ProgramParameter[] bckParms = pgm.getParameterList();
		QSYSObjectPathName qsys = new QSYSObjectPathName("%LIBL%","QCMDEXC","PGM");
		boolean rtn = true;
		try {
			pgm.setProgram(qsys.getPath());
			pgm.setParameterList(parms);
			rtn =  pgm.run();
		} catch (Exception e) {
			rtn = false;
		}finally{
			try {
				pgm.setParameterList(bckParms);
				pgm.setProgram(bckPgm);
			} catch (Exception e2) {
			}
		}
		return rtn;
	}
	
	public static boolean qcmdexc(ProgramCall pgm, String cmd, Logger logger) throws IFDException{
		ProgramParameter parms[] = new ProgramParameter[2];
		parms[0]=new ProgramParameter(new AS400Text(cmd.length(),pgm.getSystem()).toBytes(cmd));
		parms[1]=new ProgramParameter(new AS400PackedDecimal(15,5).toBytes(cmd.length()));
		String bckPgm = pgm.getProgram();
		ProgramParameter[] bckParms = pgm.getParameterList();
		QSYSObjectPathName qsys = new QSYSObjectPathName("%LIBL%","QCMDEXC","PGM");
		boolean rtn = true;
		try {
			pgm.setProgram(qsys.getPath());
			pgm.setParameterList(parms);
			rtn =  pgm.run();
		} catch (Exception e) {
			throw new IFDException(e.getLocalizedMessage(), logger);
		}finally{
			try {
				pgm.setParameterList(bckParms);
				pgm.setProgram(bckPgm);
			} catch (Exception e2) {
			}
		}
		return rtn;
	}
	
	public static boolean qcmdexc(CommandCall pgm, String cmd){
		String bckPgm = pgm.getCommand();
		boolean rtn = true;
		cmd = cmd.trim();
		try {
			rtn = pgm.run("CALL QCMDEXC PARM('" + cmd.trim() + "' " + cmd.length() + ")");
		} catch (Exception e) {
			rtn = false;
		}finally{
			try {
				pgm.setCommand(bckPgm);
			} catch (Exception e2) {
			}
		}
		return rtn;
	}
	
	public static boolean qcmdexc(CommandCall cmd, String command, Logger logger) throws IFDException{
		String bckPgm = cmd.getCommand();
		boolean rtn = true;
		command = command.trim();
		try {
			rtn = cmd.run("CALL QCMDEXC PARM('" + command.trim() + "' " + command.length() + ")");
		} catch (Exception e) {
			throw new IFDException(e.getLocalizedMessage(), logger);
		}finally{
			try {
				cmd.setCommand(bckPgm);
			} catch (Exception e2) {
			}
		}
		return rtn;
	}
	/**
	 * Using ObjectDescription to check whether the object exists.
	 * @param lib
	 * @param obj
	 * @param type
	 * @return
	 */
	public static boolean isObjectExist(String lib, String obj, String type){
		try {
			ObjectDescription chk = new ObjectDescription(IFDConnectionPool.getAS400(), 
					lib, obj, type);
			return chk.exists();
		} catch (Exception e) {
			return false;
		}
		
	}
}
